package com.bes.bessdk.scan;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;

public class BtPermission extends Activity {
    private static final int REQUEST_LOCATION_PERMISSION = 0x01;
    public Context mContext;

    public BtPermission(Context context) {
        mContext = context;
    }

    public boolean checkConditions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            return true;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            if (!initPermission())
                return false;
        }
        return initLocation();
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    private boolean initPermission() {
        if (ActivityCompat.checkSelfPermission(mContext,
                Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) mContext,
                Manifest.permission.ACCESS_COARSE_LOCATION)) {
            new AlertDialog.Builder(mContext)
                    .setMessage(
                            "Location permission is needed after Android 6.0. Apply the location permission firstly?")
                    .setNegativeButton("no", null).setPositiveButton("yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            ActivityCompat.requestPermissions((Activity) mContext,
                                    new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
                                    REQUEST_LOCATION_PERMISSION);
                        }
                    }).create().show();
        } else {
            ActivityCompat.requestPermissions((Activity) mContext, new String[] {
                    Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION },
                    REQUEST_LOCATION_PERMISSION);
        }
        return false;
    }

    private boolean initLocation() {
        LocationManager manager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
        if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            return true;
        }
        new AlertDialog.Builder(mContext).setMessage(
                "Location service is needed after Android 6.0. Turn to the setting view to enable this service?")
                .setNegativeButton("no", null).setPositiveButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        mContext.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }
                }).create().show();
        return false;
    }
}
